package com.weharvest.edu.service.Impl;

import com.weharvest.edu.config.FileConfig;
import com.weharvest.edu.dao.ReceptionDocumentDao;
import com.weharvest.edu.dto.app.AppRecordDto;
import com.weharvest.edu.entity.RecpetiopDocument;
import com.weharvest.edu.enums.CommonEnums;
import com.weharvest.edu.enums.ResultEnum;
import com.weharvest.edu.exception.EduException;
import com.weharvest.edu.service.ReceptionDocumentService;
import com.weharvest.edu.utlis.KeyUtil;
import com.weharvest.edu.utlis.WechatUtil;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.io.File;
import java.util.Date;

@Service
public class ReceptionDocumentServiceImpl implements ReceptionDocumentService {

    @Resource
    private FileConfig fileConfig;

    @Resource
    private ReceptionDocumentDao documentDao;

    @Override
    @Transactional
    public void downLoad(AppRecordDto appRecordDto, WxMpService wxMpService) {
        try {
            // serviceId和用户id不能为空
            if (appRecordDto.getServerId().equals("")
                    || appRecordDto.getUserId().equals("")) {
                throw new EduException(ResultEnum.AUDIO_DOWNLOAD_INPUT_NODATA);
            }
            String accessToken = wxMpService.getAccessToken();
            String picName = "Downs" + System.currentTimeMillis();
            String picPath = fileConfig.getAudioLoad();
            File file = new File(picPath);
            if (!file.exists()) {
                file.mkdirs();
                return;
            }
            // 下载地址
            WechatUtil.saveImageToDisk(accessToken, appRecordDto.getServerId(), picName, picPath);
            // 转换格式
            String type = "aac";
            String sourcePath = picPath + picName + ".amr";
            String targetPath = picPath + type + File.separator + picName + ".aac";
            //将amr格式转成aac格式
            WechatUtil.changeToAAC(sourcePath, targetPath);
            //记录数据
            RecpetiopDocument document = new RecpetiopDocument();
            document.setId(KeyUtil.genUniqueKey());
            document.setCreateTime(new Date());
            document.setFlag(CommonEnums.flag.FLAG_AVAILABLE.getCode());
            document.setState(CommonEnums.state.STATE_AVAILABLE.getCode());
            document.setName(picName);
            document.setType(type);
            document.setOrganizationId("100000");
            document.setCreateId(appRecordDto.getUserId());
            documentDao.save(document);
        } catch (Exception e) {
            e.printStackTrace();
            throw new EduException(ResultEnum.AUDIO_DOWNLOAD_ERROR);
        }
    }

    //音视频合并
    public void mergn(AppRecordDto appRecordDto, String mp4Path, String outPath) {
        // 读取某个用户下的这个视频系列的所有语音路径
//        documentDao.findByFlagAndStateAndCreateIdAndPath(
//                CommonEnum.FLAG_AVAILABLE.getCode(),
//                CommonEnum.STATE_AVAILABLE.getCode(),
//                appRecordDto.getUserId(),
//                appRecordDto.get);
//        WechatUtil.muxAacMp4(aacPaths,mp4Path,outPath);
    }

}
